package com.csis;

import java.sql.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.engine.SessionFactoryImplementor;
import org.junit.Test;

public class Generator extends BaseTest {
	public static final String TABLE_PREFIX = "TABLE_";
	public static final String COLUMN_PREFIX = "COLUMN_";

	String tableName = "role";
	SessionFactory sessionFactory = getBean("sessionFactory", SessionFactory.class);

	
//	@Test
	public void genBean(){
		Connection con = null;
		PreparedStatement prepStmt = null;
		ResultSet rs = null;
		ResultSetMetaData meta = null;
		String constantLinePattern = "String {0} = \"{1}\";";
		String columnNamePattern = COLUMN_PREFIX + "{0}_{1}";
		String tablePattern = TABLE_PREFIX + "{0}";
		try {
			con = ((SessionFactoryImplementor) sessionFactory).getConnectionProvider().getConnection();
			prepStmt = con.prepareStatement("SELECT * FROM " + tableName + " LIMIT 1");
			rs = prepStmt.executeQuery();
			meta = rs.getMetaData();
			List<String> constantDefine = new ArrayList<String>();
			List<String> domainDefine = new ArrayList<String>();
			String tableDefineName = MessageFormat.format(tablePattern, tableName.toUpperCase());
			String headStr = "@Entity\n@Table(name={0})\npublic class {1} ";
			domainDefine.add(MessageFormat.format(headStr, tableDefineName, tableName) + "{");
			constantDefine.add(MessageFormat.format(constantLinePattern, tableDefineName, toClassName(tableName)));
			for (int i = 1; i <= meta.getColumnCount(); i++) {
				String column = meta.getColumnName(i);
				String attr = column;
//				String columnConstant = MessageFormat.format(columnNamePattern, tableName.toUpperCase(),
//						attr.toUpperCase());
//				constantDefine.add(MessageFormat.format(constantLinePattern, columnConstant, column));
//				domainDefine.add("@Column(name =  " + columnConstant + ")");
				domainDefine.add("private "
						+ (("java.sql.Timestamp".equals(meta.getColumnClassName(i)) || "java.sql.Date".equals(meta
								.getColumnClassName(i))) ? "java.util.Date" : meta.getColumnClassName(i)) + " " + attr
						+ " = null;");
			}
			domainDefine.add("}");
			toPrint(constantDefine);
			System.out.println("\n\n");
			toPrint(domainDefine);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}

			try {
				if (prepStmt != null)
					prepStmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}

			try {
				if (con != null)
					con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	@Test
	public void genDomain() {
		Connection con = null;
		PreparedStatement prepStmt = null;
		ResultSet rs = null;
		ResultSetMetaData meta = null;
		String constantLinePattern = "String {0} = \"{1}\";";
		String columnNamePattern = COLUMN_PREFIX + "{0}_{1}";
		String tablePattern = TABLE_PREFIX + "{0}";
		try {
			con = ((SessionFactoryImplementor) sessionFactory).getConnectionProvider().getConnection();
			prepStmt = con.prepareStatement("SELECT * FROM " + tableName + " LIMIT 1");
			rs = prepStmt.executeQuery();
			meta = rs.getMetaData();
			List<String> constantDefine = new ArrayList<String>();
			List<String> domainDefine = new ArrayList<String>();
			String tableDefineName = MessageFormat.format(tablePattern, tableName.toUpperCase());
			String headStr = "@Entity\n@Table(name={0})\npublic class {1} ";
			domainDefine.add(MessageFormat.format(headStr, tableDefineName, toClassName(tableName)) + "{");
			constantDefine.add(MessageFormat.format(constantLinePattern, tableDefineName, tableName));
			for (int i = 1; i <= meta.getColumnCount(); i++) {
				String column = meta.getColumnName(i);
				String attr = toCamel(column);
				String columnConstant = MessageFormat.format(columnNamePattern, tableName.toUpperCase(),
						attr.toUpperCase());
				constantDefine.add(MessageFormat.format(constantLinePattern, columnConstant, column));
				domainDefine.add("@Column(name =  " + columnConstant + ")");
				domainDefine.add("private "
						+ (("java.sql.Timestamp".equals(meta.getColumnClassName(i)) || "java.sql.Date".equals(meta
								.getColumnClassName(i))) ? "java.util.Date" : meta.getColumnClassName(i)) + " " + attr
						+ " = null;");
			}
			domainDefine.add("}");
			toPrint(constantDefine);
			System.out.println("\n\n");
			toPrint(domainDefine);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}

			try {
				if (prepStmt != null)
					prepStmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}

			try {
				if (con != null)
					con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void toPrint(List<String> list) {
		for (String string : list) {
			System.out.println(string);
		}
	}

	private String toCamel(String column) {
		return _toCamel(column.toUpperCase());
	}

	public String toClassName(String tableName) {
		String camel = "";
		if (tableName == null || tableName.trim().equals(""))
			return null;
		if (tableName.charAt(0) == '_' || tableName.charAt(tableName.length() - 1) == '_')
			throw new IllegalArgumentException("actionType不能以下划线开始或者结束！");
		tableName = tableName.toLowerCase();
		String[] tmp = tableName.split("_");
		for (int i = 0; i < tmp.length; i++) {
			camel += ("" + tmp[i].charAt(0)).toUpperCase() + tmp[i].substring(1);
		}
		return camel;
	}

	public static String _toCamel(String upper) {
		String camel = "";
		if (upper == null || upper.trim().equals(""))
			return null;
		if (upper.charAt(0) == '_' || upper.charAt(upper.length() - 1) == '_')
			throw new IllegalArgumentException("actionType不能以下划线开始或者结束！");
		upper = upper.toLowerCase();
		String[] tmp = upper.split("_");
		camel += tmp[0];
		for (int i = 1; i < tmp.length; i++) {
			camel += ("" + tmp[i].charAt(0)).toUpperCase() + tmp[i].substring(1);
		}
		return camel;
	}
}
